package com.zhida.smartcontrol.dialog.switchlightdialog;

import android.text.TextUtils;

import com.zhida.smartcontrol.common.Api;
import com.zhida.smartcontrol.common.bean.LampSwitchBean;
import com.zhida.smartcontrol.knet.KalleCallBack;
import com.zhida.smartcontrol.mvp.BasePresenterImpl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SwitchLightDialogPresenter extends BasePresenterImpl<SwitchLightDialogContract.View> implements SwitchLightDialogContract.Presenter {

    @Override
    public void openCloseNb(List<LampSwitchBean> codeList, String type) {
        if (TextUtils.isEmpty(type)) {
            mView.showMsg("请选择开灯或关灯");
            return;
        }
        // NB单灯
        List<String> list = new ArrayList<>();
        for (LampSwitchBean lampBean : codeList) {
            list.add(lampBean.getCode());
        }
        Map<String, Object> params = new HashMap<>();
        params.put("codeList", list);
        params.put("type", type);// 1：开启（灯） 0：关闭（灯）
        mKalleCenter.postJson(Api.OPEN_CLOSE_SWITCH, params, true, new KalleCallBack() {
            @Override
            public void onSuccess(String result, String msg) {
                mView.showMsg(msg);
                mView.onSuccess();
            }

            @Override
            public void onFailed(String msg) {
                mView.showMsg(msg);
            }
        });
    }

    @Override
    public void openCloseNbDouble(List<LampSwitchBean> codeList, String value) {
        if (TextUtils.isEmpty(value)) {
            mView.showMsg("请选择开灯或关灯");
            return;
        }
        List<String> list = new ArrayList<>();
        for (LampSwitchBean lampBean : codeList) {
            list.add(lampBean.getCode());
        }
        String type = "";
        LampSwitchBean bean = codeList.get(0);
        if (bean.isCheck()) {
            type = "3";
        } else if (bean.isCheckMain() && !bean.isCheckAux()) {
            type = "1";
        } else if (!bean.isCheckMain() && bean.isCheckAux()) {
            type = "2";
        }
        Map<String, Object> params = new HashMap<>();
        params.put("codeList", list);
        params.put("type", type);//type  1：主灯2：辅灯3：主辅灯全选
        params.put("value", value);// 1：开启（灯） 0：关闭（灯）
        mKalleCenter.postJson(Api.OPEN_CLOSE_NB_DOUBLE, params, true, new KalleCallBack() {
            @Override
            public void onSuccess(String result, String msg) {
                mView.showMsg(msg);
                mView.onSuccess();
            }

            @Override
            public void onFailed(String msg) {
                mView.showMsg(msg);
            }
        });
    }

    @Override
    public void openClosePlcZig(List<LampSwitchBean> codeList, String type) {
        if (TextUtils.isEmpty(type)) {
            mView.showMsg("请选择开灯或关灯");
            return;
        }
        // NB单灯
        List<String> list = new ArrayList<>();
        for (LampSwitchBean lampBean : codeList) {
            list.add(lampBean.getId());
        }
        Map<String, Object> params = new HashMap<>();
        params.put("idList", list);
        params.put("type", type);// 1：开启（灯） 0：关闭（灯）
        mKalleCenter.postJson(Api.OPEN_CLOSE_P_Z, params, true, new KalleCallBack() {
            @Override
            public void onSuccess(String result, String msg) {
                mView.showMsg(msg);
                mView.onSuccess();
            }

            @Override
            public void onFailed(String msg) {
                mView.showMsg(msg);
            }
        });
    }

}
